#include "SSAOMapFX.h"

SSAOMapFX::SSAOMapFX(ID3D11Device* device)
	: AbstractFX(device, L"FX/SSAOMap.fxo")
{
	InitInputLayout(device);
	InitShaderVariablePointers();
};

SSAOMapFX::~SSAOMapFX()
{}

void SSAOMapFX::InitInputLayout(ID3D11Device* device)
{
	D3D11_INPUT_ELEMENT_DESC inputLayout[] = 
	{
		{"POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,       0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"INDEX",    0, DXGI_FORMAT_R32_FLOAT,          0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0}
	};

	CreateInputLayout(device, inputLayout, 3);
}

void SSAOMapFX::InitShaderVariablePointers()
{
	viewTexVar = effect->GetVariableByName("viewToTexSpace")->AsMatrix();
	offsetVectorArrVar = effect->GetVariableByName("offsetVectors")->AsVector();
	frustumCornerArrVar = effect->GetVariableByName("frustumCorners")->AsVector();

	viewspaceNormalDepthMapVar = effect->GetVariableByName("viewSpaceNormalDepthMap")->AsShaderResource();
	randomVectorMapVar = effect->GetVariableByName("randomVectorMap")->AsShaderResource();
}

void SSAOMapFX::SetCBufferPerFrame(CXMMATRIX viewToTexSpaceMatrix, 
		XMFLOAT4 *offset, XMFLOAT4 *corners)
{
	viewTexVar->SetMatrix(reinterpret_cast<const float*>(&viewToTexSpaceMatrix));
	offsetVectorArrVar->SetFloatVectorArray(reinterpret_cast<const float*>(offset), 0, 14);
	frustumCornerArrVar->SetFloatVectorArray(reinterpret_cast<const float*>(corners),0, 4);
}

void SSAOMapFX::SetShaderResources(ID3D11ShaderResourceView* viewspaceNormalDepth,
		ID3D11ShaderResourceView* randomVectorMap)
{
	viewspaceNormalDepthMapVar->SetResource(viewspaceNormalDepth);
	randomVectorMapVar->SetResource(randomVectorMap);
}